home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / os2 / adaptor.zip / ADAPT.ZIP / adaptor / src / controlf.c < prev    next >
Text File  |  1994-01-03  |  14KB  |  631 lines

  1. # include "ControlF.h"
  2. # include "yyCFlow.w"
  3. # include <stdio.h>
  4. # if defined __STDC__ | defined __cplusplus
  5. #  include <stdlib.h>
  6. # else
  7.    extern void exit ();
  8. # endif
  9. # include "Tree.h"
  10.  
  11. # ifndef NULL
  12. # define NULL 0L
  13. # endif
  14. # ifndef false
  15. # define false 0
  16. # endif
  17. # ifndef true
  18. # define true 1
  19. # endif
  20.  
  21. # ifdef yyInline
  22. # define yyALLOC(tree, free, max, alloc, nodesize, make, ptr, kind) \
  23.   if ((ptr = (tree) free) >= (tree) max) ptr = alloc (); \
  24.   free += nodesize [kind]; \
  25.   ptr->yyHead.yyMark = 0; \
  26.   ptr->Kind = kind;
  27. # else
  28. # define yyALLOC(tree, free, max, alloc, nodesize, make, ptr, kind) ptr = make (kind);
  29. # endif
  30.  
  31. # define yyWrite(s) (void) fputs (s, yyf)
  32. # define yyWriteNl (void) fputc ('\n', yyf)
  33.  
  34. # line 21 "ControlFlow.puma"
  35.  
  36. # include "Idents.h"
  37. # include "StringMe.h"
  38.  
  39. # include "protocol.h"
  40.  
  41. # include "Types.h"
  42. # include "Transfor.h"     /* ReplaceACF */
  43.  
  44. /*********************************************************************
  45. *                                                                    *
  46. *  Global Data for Control Flow Analysis                             *
  47. *                                                                    *
  48. *********************************************************************/
  49.  
  50. int MaxLabel;
  51. int NoReturns;
  52.  
  53. void SetMaxLabel (label)
  54. int label;
  55. { if (label > MaxLabel) MaxLabel = label;
  56. } /* SetMaxLabel */
  57.  
  58.  
  59.  
  60. static FILE * yyf = stdout;
  61.  
  62. static void yyAbort
  63. # ifdef __cplusplus
  64.  (char * yyFunction)
  65. # else
  66.  (yyFunction) char * yyFunction;
  67. # endif
  68. {
  69.  (void) fprintf (stderr, "Error: module ControlFlow, routine %s failed\n", yyFunction);
  70.  exit (1);
  71. }
  72.  
  73. void ControlFlow ARGS((tTree t));
  74. static void CheckUpLabels ARGS((tTree t));
  75. static tTree RemoveLastReturn ARGS((tTree t));
  76. static tTree AppendEndLabel ARGS((tTree t, int label));
  77. static void ReplaceReturn ARGS((tTree t, int gotolabel));
  78.  
  79. void ControlFlow
  80. # if defined __STDC__ | defined __cplusplus
  81. (register tTree t)
  82. # else
  83. (t)
  84.  register tTree t;
  85. # endif
  86. {
  87.   if (t == NoTree) return;
  88.  
  89.   switch (t->Kind) {
  90.   case kCOMP_UNIT:
  91. # line 54 "ControlFlow.puma"
  92.   {
  93. # line 55 "ControlFlow.puma"
  94.    open_protocol ("adaptor.cf");
  95. # line 56 "ControlFlow.puma"
  96.    ControlFlow (t->COMP_UNIT.COMP_ELEMENTS);
  97. # line 57 "ControlFlow.puma"
  98.    close_protocol ();
  99.   }
  100.    return;
  101.  
  102.   case kDECL_EMPTY:
  103. # line 62 "ControlFlow.puma"
  104.    return;
  105.  
  106.   case kDECL_LIST:
  107. # line 65 "ControlFlow.puma"
  108.   {
  109. # line 66 "ControlFlow.puma"
  110.    ControlFlow (t->DECL_LIST.Elem);
  111. # line 67 "ControlFlow.puma"
  112.    ControlFlow (t->DECL_LIST.Next);
  113.   }
  114.    return;
  115.  
  116.   case kPROGRAM_DECL:
  117. # line 77 "ControlFlow.puma"
  118.   {
  119. # line 78 "ControlFlow.puma"
  120.    set_protocol_unit (t);
  121. # line 79 "ControlFlow.puma"
  122.    ControlFlow (t->PROGRAM_DECL.PROGRAM_BODY);
  123.   }
  124.    return;
  125.  
  126.   case kPROC_DECL:
  127. # line 82 "ControlFlow.puma"
  128.   {
  129. # line 83 "ControlFlow.puma"
  130.    set_protocol_unit (t);
  131. # line 84 "ControlFlow.puma"
  132.    ControlFlow (t->PROC_DECL.PROC_BODY);
  133.   }
  134.    return;
  135.  
  136.   case kFUNC_DECL:
  137. # line 87 "ControlFlow.puma"
  138.   {
  139. # line 88 "ControlFlow.puma"
  140.    set_protocol_unit (t);
  141. # line 89 "ControlFlow.puma"
  142.    ControlFlow (t->FUNC_DECL.FUNC_BODY);
  143.   }
  144.    return;
  145.  
  146.   case kMODULE_DECL:
  147. # line 92 "ControlFlow.puma"
  148.   {
  149. # line 93 "ControlFlow.puma"
  150.    set_protocol_unit (t);
  151. # line 94 "ControlFlow.puma"
  152.    ControlFlow (t->MODULE_DECL.MODULE_BODY);
  153.   }
  154.    return;
  155.  
  156.   case kBLOCK_DATA_DECL:
  157. # line 97 "ControlFlow.puma"
  158.   {
  159. # line 98 "ControlFlow.puma"
  160.    set_protocol_unit (t);
  161. # line 99 "ControlFlow.puma"
  162.    ControlFlow (t->BLOCK_DATA_DECL.DATA_BODY);
  163.   }
  164.    return;
  165.  
  166.   case kBODY_NODE:
  167. # line 112 "ControlFlow.puma"
  168.   {
  169. # line 116 "ControlFlow.puma"
  170.    MaxLabel = 0;
  171. # line 117 "ControlFlow.puma"
  172.    NoReturns = 0;
  173. # line 121 "ControlFlow.puma"
  174.    CheckUpLabels (t->BODY_NODE.STATS);
  175. # line 124 "ControlFlow.puma"
  176.  if (NoReturns > 0)
  177.         t->BODY_NODE.STATS = RemoveLastReturn (t->BODY_NODE.STATS);
  178. # line 129 "ControlFlow.puma"
  179.  if (NoReturns > 0)
  180.         { t->BODY_NODE.STATS = AppendEndLabel (t->BODY_NODE.STATS, MaxLabel + 1);
  181.           ReplaceReturn (t->BODY_NODE.STATS, MaxLabel+1);   }
  182.  
  183. # line 136 "ControlFlow.puma"
  184.  if (NoReturns > 0)
  185.        simple_error_protocol ("not all returns removed");
  186.  
  187.   }
  188.    return;
  189.  
  190.   }
  191.  
  192. ;
  193. }
  194.  
  195. static void CheckUpLabels
  196. # if defined __STDC__ | defined __cplusplus
  197. (register tTree t)
  198. # else
  199. (t)
  200.  register tTree t;
  201. # endif
  202. {
  203.   if (t == NoTree) return;
  204.  
  205.   switch (t->Kind) {
  206.   case kACF_LIST:
  207. # line 152 "ControlFlow.puma"
  208.   {
  209. # line 153 "ControlFlow.puma"
  210.    set_protocol_stmt (t->ACF_LIST.Elem);
  211. # line 154 "ControlFlow.puma"
  212.    CheckUpLabels (t->ACF_LIST.Elem);
  213. # line 155 "ControlFlow.puma"
  214.    CheckUpLabels (t->ACF_LIST.Next);
  215.   }
  216.    return;
  217.  
  218.   case kACF_EMPTY:
  219. # line 158 "ControlFlow.puma"
  220.    return;
  221.  
  222.   case kACF_DUMMY:
  223. # line 161 "ControlFlow.puma"
  224.   {
  225. # line 162 "ControlFlow.puma"
  226.    SetMaxLabel (t->ACF_DUMMY.Label);
  227.   }
  228.    return;
  229.  
  230.   case kACF_BASIC:
  231.   if (t->ACF_BASIC.BASIC_STMT->Kind == kRETURN_STMT) {
  232.   if (t->ACF_BASIC.BASIC_STMT->RETURN_STMT.RETURN_EXP->Kind == kDUMMY_EXP) {
  233. # line 165 "ControlFlow.puma"
  234.   {
  235. # line 166 "ControlFlow.puma"
  236.    SetMaxLabel (t->ACF_BASIC.Label);
  237. # line 167 "ControlFlow.puma"
  238.    NoReturns = NoReturns + 1;
  239.   }
  240.    return;
  241.  
  242.   }
  243. # line 170 "ControlFlow.puma"
  244.   {
  245. # line 171 "ControlFlow.puma"
  246.    SetMaxLabel (t->ACF_BASIC.Label);
  247. # line 172 "ControlFlow.puma"
  248.    error_protocol ("Alternative return is rejected\n");
  249.   }
  250.    return;
  251.  
  252.   }
  253. # line 175 "ControlFlow.puma"
  254.   {
  255. # line 176 "ControlFlow.puma"
  256.    SetMaxLabel (t->ACF_BASIC.Label);
  257.   }
  258.    return;
  259.  
  260.   case kACF_IF:
  261. # line 179 "ControlFlow.puma"
  262.   {
  263. # line 180 "ControlFlow.puma"
  264.    SetMaxLabel (t->ACF_IF.Label);
  265. # line 181 "ControlFlow.puma"
  266.    CheckUpLabels (t->ACF_IF.THEN_PART);
  267. # line 182 "ControlFlow.puma"
  268.    CheckUpLabels (t->ACF_IF.ELSE_PART);
  269.   }
  270.    return;
  271.  
  272.   case kACF_WHERE:
  273. # line 185 "ControlFlow.puma"
  274.   {
  275. # line 186 "ControlFlow.puma"
  276.    SetMaxLabel (t->ACF_WHERE.Label);
  277. # line 187 "ControlFlow.puma"
  278.    CheckUpLabels (t->ACF_WHERE.TRUE_PART);
  279. # line 188 "ControlFlow.puma"
  280.    CheckUpLabels (t->ACF_WHERE.FALSE_PART);
  281.   }
  282.    return;
  283.  
  284.   case kACF_CASE:
  285. # line 191 "ControlFlow.puma"
  286.   {
  287. # line 192 "ControlFlow.puma"
  288.    SetMaxLabel (t->ACF_CASE.Label);
  289. # line 193 "ControlFlow.puma"
  290.    CheckUpLabels (t->ACF_CASE.CASE_ALTS);
  291. # line 194 "ControlFlow.puma"
  292.    CheckUpLabels (t->ACF_CASE.CASE_OTHERWISE);
  293.   }
  294.    return;
  295.  
  296.   case kSELECTED_ACF_LIST:
  297. # line 197 "ControlFlow.puma"
  298.   {
  299. # line 198 "ControlFlow.puma"
  300.    CheckUpLabels (t->SELECTED_ACF_LIST.Elem);
  301. # line 199 "ControlFlow.puma"
  302.    CheckUpLabels (t->SELECTED_ACF_LIST.Next);
  303.   }
  304.    return;
  305.  
  306.   case kSELECTED_ACF_EMPTY:
  307. # line 202 "ControlFlow.puma"
  308.    return;
  309.  
  310.   case kSELECTED_ACF_NODE:
  311. # line 205 "ControlFlow.puma"
  312.   {
  313. # line 206 "ControlFlow.puma"
  314.    CheckUpLabels (t->SELECTED_ACF_NODE.SELECT_ACFS);
  315.   }
  316.    return;
  317.  
  318.   case kACF_WHILE:
  319. # line 209 "ControlFlow.puma"
  320.   {
  321. # line 210 "ControlFlow.puma"
  322.    SetMaxLabel (t->ACF_WHILE.Label);
  323. # line 211 "ControlFlow.puma"
  324.    CheckUpLabels (t->ACF_WHILE.WHILE_BODY);
  325.   }
  326.    return;
  327.  
  328.   case kACF_DOLOCAL:
  329. # line 214 "ControlFlow.puma"
  330.   {
  331. # line 215 "ControlFlow.puma"
  332.    SetMaxLabel (t->ACF_DOLOCAL.Label);
  333. # line 216 "ControlFlow.puma"
  334.    CheckUpLabels (t->ACF_DOLOCAL.DOLOCAL_BODY);
  335.   }
  336.    return;
  337.  
  338.   case kACF_DOALL:
  339. # line 219 "ControlFlow.puma"
  340.   {
  341. # line 220 "ControlFlow.puma"
  342.    SetMaxLabel (t->ACF_DOALL.Label);
  343. # line 221 "ControlFlow.puma"
  344.    CheckUpLabels (t->ACF_DOALL.DOALL_BODY);
  345.   }
  346.    return;
  347.  
  348.   case kACF_FORALL:
  349. # line 224 "ControlFlow.puma"
  350.   {
  351. # line 225 "ControlFlow.puma"
  352.    SetMaxLabel (t->ACF_FORALL.Label);
  353. # line 226 "ControlFlow.puma"
  354.    CheckUpLabels (t->ACF_FORALL.FORALL_BODY);
  355.   }
  356.    return;
  357.  
  358.   case kACF_DO:
  359. # line 229 "ControlFlow.puma"
  360.   {
  361. # line 230 "ControlFlow.puma"
  362.    SetMaxLabel (t->ACF_DO.Label);
  363. # line 231 "ControlFlow.puma"
  364.    CheckUpLabels (t->ACF_DO.DO_BODY);
  365.   }
  366.    return;
  367.  
  368.   case kACF_ENTRY:
  369. # line 234 "ControlFlow.puma"
  370.   {
  371. # line 235 "ControlFlow.puma"
  372.    SetMaxLabel (t->ACF_ENTRY.Label);
  373.   }
  374.    return;
  375.  
  376.   }
  377.  
  378. # line 238 "ControlFlow.puma"
  379.   {
  380. # line 239 "ControlFlow.puma"
  381.    printf ("Unknown Tree in ControlFlow: CheckUpLabels\n");
  382. # line 240 "ControlFlow.puma"
  383.    WriteTree (stdout, t);
  384. # line 241 "ControlFlow.puma"
  385.    kill_in_protocol ();
  386.   }
  387.    return;
  388.  
  389. ;
  390. }
  391.  
  392. static tTree RemoveLastReturn
  393. # if defined __STDC__ | defined __cplusplus
  394. (register tTree t)
  395. # else
  396. (t)
  397.  register tTree t;
  398. # endif
  399. {
  400.   if (t->Kind == kACF_LIST) {
  401.   if (t->ACF_LIST.Elem->Kind == kACF_BASIC) {
  402.   if (t->ACF_LIST.Elem->ACF_BASIC.BASIC_STMT->Kind == kRETURN_STMT) {
  403.   if (t->ACF_LIST.Elem->ACF_BASIC.BASIC_STMT->RETURN_STMT.RETURN_EXP->Kind == kDUMMY_EXP) {
  404.   if (t->ACF_LIST.Next->Kind == kACF_EMPTY) {
  405. # line 255 "ControlFlow.puma"
  406.   {
  407. # line 257 "ControlFlow.puma"
  408.    NoReturns = NoReturns - 1;
  409.   }
  410.    return ReplaceACF (t, NoTree, t->ACF_LIST.Next);
  411.  
  412.   }
  413.   }
  414.   }
  415.   }
  416. # line 262 "ControlFlow.puma"
  417.   {
  418. # line 263 "ControlFlow.puma"
  419.  t->ACF_LIST.Next = RemoveLastReturn (t->ACF_LIST.Next);
  420.   }
  421.    return t;
  422.  
  423.   }
  424.   if (t->Kind == kACF_EMPTY) {
  425. # line 267 "ControlFlow.puma"
  426.    return t;
  427.  
  428.   }
  429.  yyAbort ("RemoveLastReturn");
  430. }
  431.  
  432. static tTree AppendEndLabel
  433. # if defined __STDC__ | defined __cplusplus
  434. (register tTree t, register int label)
  435. # else
  436. (t, label)
  437.  register tTree t;
  438.  register int label;
  439. # endif
  440. {
  441. # line 283 "ControlFlow.puma"
  442.  
  443. tTree new;
  444.  
  445.   if (t->Kind == kACF_EMPTY) {
  446. # line 287 "ControlFlow.puma"
  447.   {
  448. # line 288 "ControlFlow.puma"
  449.  new = mACF_DUMMY ();
  450.      new->ACF_NODE.Label = label;
  451.      new->ACF_NODE.Line  = 0;
  452.      new = mACF_LIST (new, t);
  453.  
  454.   }
  455.    return new;
  456.  
  457.   }
  458.   if (t->Kind == kACF_LIST) {
  459. # line 296 "ControlFlow.puma"
  460.   {
  461. # line 297 "ControlFlow.puma"
  462.  t->ACF_LIST.Next = AppendEndLabel (t->ACF_LIST.Next, label);
  463.   }
  464.    return t;
  465.  
  466.   }
  467.  yyAbort ("AppendEndLabel");
  468. }
  469.  
  470. static void ReplaceReturn
  471. # if defined __STDC__ | defined __cplusplus
  472. (register tTree t, register int gotolabel)
  473. # else
  474. (t, gotolabel)
  475.  register tTree t;
  476.  register int gotolabel;
  477. # endif
  478. {
  479.   if (t == NoTree) return;
  480.  
  481.   switch (t->Kind) {
  482.   case kACF_LIST:
  483. # line 315 "ControlFlow.puma"
  484.   {
  485. # line 316 "ControlFlow.puma"
  486.    ReplaceReturn (t->ACF_LIST.Elem, gotolabel);
  487. # line 317 "ControlFlow.puma"
  488.    ReplaceReturn (t->ACF_LIST.Next, gotolabel);
  489.   }
  490.    return;
  491.  
  492.   case kACF_EMPTY:
  493. # line 320 "ControlFlow.puma"
  494.    return;
  495.  
  496.   case kACF_DUMMY:
  497. # line 323 "ControlFlow.puma"
  498.    return;
  499.  
  500.   case kACF_BASIC:
  501.   if (t->ACF_BASIC.BASIC_STMT->Kind == kRETURN_STMT) {
  502.   if (t->ACF_BASIC.BASIC_STMT->RETURN_STMT.RETURN_EXP->Kind == kDUMMY_EXP) {
  503. # line 326 "ControlFlow.puma"
  504.   {
  505. # line 327 "ControlFlow.puma"
  506.  t->ACF_BASIC.BASIC_STMT = mGOTO_STMT (gotolabel);
  507. # line 328 "ControlFlow.puma"
  508.    NoReturns = NoReturns - 1;
  509.   }
  510.    return;
  511.  
  512.   }
  513. # line 331 "ControlFlow.puma"
  514.    return;
  515.  
  516.   }
  517. # line 335 "ControlFlow.puma"
  518.    return;
  519.  
  520.   case kACF_IF:
  521. # line 338 "ControlFlow.puma"
  522.   {
  523. # line 339 "ControlFlow.puma"
  524.    ReplaceReturn (t->ACF_IF.THEN_PART, gotolabel);
  525. # line 340 "ControlFlow.puma"
  526.    ReplaceReturn (t->ACF_IF.ELSE_PART, gotolabel);
  527.   }
  528.    return;
  529.  
  530.   case kACF_WHERE:
  531. # line 343 "ControlFlow.puma"
  532.   {
  533. # line 344 "ControlFlow.puma"
  534.    ReplaceReturn (t->ACF_WHERE.TRUE_PART, gotolabel);
  535. # line 345 "ControlFlow.puma"
  536.    ReplaceReturn (t->ACF_WHERE.FALSE_PART, gotolabel);
  537.   }
  538.    return;
  539.  
  540.   case kACF_CASE:
  541. # line 348 "ControlFlow.puma"
  542.   {
  543. # line 349 "ControlFlow.puma"
  544.    ReplaceReturn (t->ACF_CASE.CASE_ALTS, gotolabel);
  545. # line 350 "ControlFlow.puma"
  546.    ReplaceReturn (t->ACF_CASE.CASE_OTHERWISE, gotolabel);
  547.   }
  548.    return;
  549.  
  550.   case kSELECTED_ACF_LIST:
  551. # line 353 "ControlFlow.puma"
  552.   {
  553. # line 354 "ControlFlow.puma"
  554.    ReplaceReturn (t->SELECTED_ACF_LIST.Elem, gotolabel);
  555. # line 355 "ControlFlow.puma"
  556.    ReplaceReturn (t->SELECTED_ACF_LIST.Next, gotolabel);
  557.   }
  558.    return;
  559.  
  560.   case kSELECTED_ACF_EMPTY:
  561. # line 358 "ControlFlow.puma"
  562.    return;
  563.  
  564.   case kSELECTED_ACF_NODE:
  565. # line 361 "ControlFlow.puma"
  566.   {
  567. # line 362 "ControlFlow.puma"
  568.    ReplaceReturn (t->SELECTED_ACF_NODE.SELECT_ACFS, gotolabel);
  569.   }
  570.    return;
  571.  
  572.   case kACF_WHILE:
  573. # line 365 "ControlFlow.puma"
  574.   {
  575. # line 366 "ControlFlow.puma"
  576.    ReplaceReturn (t->ACF_WHILE.WHILE_BODY, gotolabel);
  577.   }
  578.    return;
  579.  
  580.   case kACF_DOLOCAL:
  581. # line 369 "ControlFlow.puma"
  582.   {
  583. # line 370 "ControlFlow.puma"
  584.    ReplaceReturn (t->ACF_DOLOCAL.DOLOCAL_BODY, gotolabel);
  585.   }
  586.    return;
  587.  
  588.   case kACF_FORALL:
  589. # line 373 "ControlFlow.puma"
  590.   {
  591. # line 374 "ControlFlow.puma"
  592.    ReplaceReturn (t->ACF_FORALL.FORALL_BODY, gotolabel);
  593.   }
  594.    return;
  595.  
  596.   case kACF_DO:
  597. # line 377 "ControlFlow.puma"
  598.   {
  599. # line 378 "ControlFlow.puma"
  600.    ReplaceReturn (t->ACF_DO.DO_BODY, gotolabel);
  601.   }
  602.    return;
  603.  
  604.   case kACF_ENTRY:
  605. # line 381 "ControlFlow.puma"
  606.    return;
  607.  
  608.   }
  609.  
  610. # line 384 "ControlFlow.puma"
  611.   {
  612. # line 385 "ControlFlow.puma"
  613.    printf ("Unknown Tree in ControlFlow: ReplaceReturn\n");
  614. # line 386 "ControlFlow.puma"
  615.    WriteTree (stdout, t);
  616. # line 387 "ControlFlow.puma"
  617.    kill_in_protocol ();
  618.   }
  619.    return;
  620.  
  621. ;
  622. }
  623.  
  624. void BeginControlFlow ()
  625. {
  626. }
  627.  
  628. void CloseControlFlow ()
  629. {
  630. }
  631.